/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tmiranda.afd;

import sagex.api.ShowAPI;
import sagex.api.UserRecordAPI;

/**
 *
 * @author Tom Miranda.
 * 
 * Use this class for database access.  Almost all methods require a Sage Show Object.
 * 
 * Use the Sage UserRecord API to store all data.  
 * - The Store name is STORE.
 * - The key for each record is the ShowTitle.
 * - Each record has various pieces of data.
 */
public class DataStore {
    
    static final String STORE               = "tmiranda.afd";   // UserRecord DataStore name.  UserRecord Key is the Show Title.
    static final String NAME_TITLE          = "showTitle";      // Used to store the Key (the ExternalID) in the UserRecord.
    static final String NAME_ID             = "externalID";     // Show ExternalID, in case we need to fetch the actual Show from the Sage database.
    static final String NAME_FAV            = "isFavorite";     // UserRecord data Name.
    static final String NAME_MARK_WATCHED   = "markWatched";
    static final String NAME_MARK_DONTLIKE  = "markDontLike";
    
    private Object show;
    private Object record;
    
    DataStore(Object Show) {
        
        show = Show;
        
        if (Show == null) {
            Log.getInstance().write(Log.LOGLEVEL_WARN, "DataStore: null Show.");
            record = null;
        } else {
            record = getRecordForShow();
            
            if (record == null)
                Log.getInstance().write(Log.LOGLEVEL_VERBOSE, "DataStore: null record.");
        }    
    }
    
    DataStore() {
        show = null;
    } 
    
    // Returns the entire UserRecord for the Show or null if none exists.
    private Object getRecordForShow() {
        
        if (show==null || ShowAPI.GetShowTitle(show)==null) {
            Log.getInstance().write(Log.LOGLEVEL_WARN, "getRecordForShow: null Show or Title.");
            return null;
        }
        
        return UserRecordAPI.GetUserRecord(STORE, ShowAPI.GetShowTitle(show));
    }
    
    // Creates a new UserRecord for the Show. Set default values for MARK_WATCHED and MARK_DONTLIKE.
    private boolean createRecordForShow() {
        if (show == null || ShowAPI.GetShowTitle(show)==null) {
            Log.getInstance().write(Log.LOGLEVEL_WARN, "createRecordForShow: null Show or Title.");
            return false;
        }
        
        Object record = UserRecordAPI.AddUserRecord(STORE, ShowAPI.GetShowTitle(show));
        
        if (record == null) {
            Log.getInstance().write(Log.LOGLEVEL_WARN, "createRecordForShow: null record.");
            return false;
        }
        
        // Store the Show ExternalID in case we need to fetch the Show from the Sage database.
        UserRecordAPI.SetUserRecordData(record, NAME_ID, ShowAPI.GetShowExternalID(show));
        UserRecordAPI.SetUserRecordData(record, NAME_TITLE, ShowAPI.GetShowTitle(show));
        UserRecordAPI.SetUserRecordData(record, NAME_MARK_WATCHED, "true");
        UserRecordAPI.SetUserRecordData(record, NAME_MARK_DONTLIKE, "true");
    
        Log.getInstance().write(Log.LOGLEVEL_TRACE, "createRecordForShow: Record created for " + ShowAPI.GetShowExternalID(show) + " " + ShowAPI.GetShowTitle(show));
        return true;
    }
    
    // Removes the UserRecord for the Show.
    boolean removeRecordForShow() {
        if (show == null) {
            Log.getInstance().write(Log.LOGLEVEL_WARN, "removeRecordForShow: null Show.");
            return false;
        }
        
        //Object Record = getRecordForShow(Show);
        
        if (record == null) {
            Log.getInstance().write(Log.LOGLEVEL_WARN, "removeRecordForShow: null Record.");
            return false;
        } else {
            Log.getInstance().write(Log.LOGLEVEL_TRACE, "removeRecordForShow: Deleting Record.");
            return UserRecordAPI.DeleteUserRecord(record);
        }
    }
      
    // Returns the data in the NAME field, which is true for an anti-favorite or false (or null) for a non-anti-favorite.
    String getAntiFavoriteStatus() {
        
        if (show == null) {
            Log.getInstance().write(Log.LOGLEVEL_WARN, "getAntiFavoriteStatus: null Show.");
            return null;
        }
        
        //Object record = getRecordForShow(Show);
        
        if (record == null) {
            return null;
        } else {
            Log.getInstance().write(Log.LOGLEVEL_VERBOSE, "getAntiFavoriteStatus: Returning status " + UserRecordAPI.GetUserRecordData(record, NAME_FAV));
            return UserRecordAPI.GetUserRecordData(record, NAME_FAV);
        }
    }
    
    // Set the anti-favorite status.
    boolean setAntiFavoriteStatus(String Status) {
        return setNameStatus(NAME_FAV, Status);
    }
    
    boolean setMarkWatchedStatus(String Status) {
        return setNameStatus(NAME_MARK_WATCHED, Status);
    }
    
    String getMarkWatchedStatus() {
        
        if (show == null) {
            Log.getInstance().write(Log.LOGLEVEL_WARN, "getMarkWatchedStatus: null Show.");
            return null;
        }
        
        //Object record = getRecordForShow(Show);
        
        if (record == null) {
            return null;
        } else {
            Log.getInstance().write(Log.LOGLEVEL_VERBOSE, "getMarkWatchedStatus: Returning status " + UserRecordAPI.GetUserRecordData(record, NAME_MARK_WATCHED));
            return UserRecordAPI.GetUserRecordData(record, NAME_MARK_WATCHED);
        }
    }    
    
    boolean setMarkDontLikeStatus(String Status) {
        return setNameStatus(NAME_MARK_DONTLIKE, Status);
    }
    
    String getMarkDontLikeStatus() {
        
        if (show == null) {
            Log.getInstance().write(Log.LOGLEVEL_WARN, "getMarkDontLikeStatus: null Show.");
            return null;
        }
        
        //Object record = getRecordForShow(Show);
        
        if (record == null) {
            return null;
        } else {
            Log.getInstance().write(Log.LOGLEVEL_VERBOSE, "getMarkDontLikeStatus: Returning status " + UserRecordAPI.GetUserRecordData(record, NAME_MARK_DONTLIKE));
            return UserRecordAPI.GetUserRecordData(record, NAME_MARK_DONTLIKE);
        }
    }    
    
    // General method to set the Name to Status.  Name can be any valid Name for the UserRecord and Status can be any String.
    private boolean setNameStatus(String Name, String Status) {
        if (show == null || Name == null) {
            Log.getInstance().write(Log.LOGLEVEL_WARN, "setNameStatus: null Show or Name.");
            return false;
        }
        
        //Object record = getRecordForShow(Show);   
        
        if (record == null) {
            Log.getInstance().write(Log.LOGLEVEL_TRACE, "setNameStatus: null Record, creating one.");
            if (createRecordForShow()) {
                record = getRecordForShow();
            
                if (record == null) {
                    Log.getInstance().write(Log.LOGLEVEL_WARN, "setNameStatus: Failed to get Record.");
                    return false;
                }         
            } else {
                Log.getInstance().write(Log.LOGLEVEL_WARN, "setNameStatus: Failed to create Record.");
                return false;
            }
        }        
               
        Log.getInstance().write(Log.LOGLEVEL_TRACE, "setNameStatus: Setting status to '" + Name + " " + Status + "'");
        UserRecordAPI.SetUserRecordData(record, Name, Status);
        return true;        
    }
    
    String getTitle() {
        
        if (show == null) {
            Log.getInstance().write(Log.LOGLEVEL_WARN, "getTitle: null Show.");
            return null;
        }
        
        //Object record = getRecordForShow(Show);
        
        if (record == null) {
            return null;
        } else {
            Log.getInstance().write(Log.LOGLEVEL_VERBOSE, "getTitle: Returning status " + UserRecordAPI.GetUserRecordData(record, NAME_TITLE));
            return UserRecordAPI.GetUserRecordData(record, NAME_TITLE);
        }
    }
    
    String getID() {
        
        if (show == null) {
            Log.getInstance().write(Log.LOGLEVEL_WARN, "getID: null Show.");
            return null;
        }
        
        //Object record = getRecordForShow(Show);
        
        if (record == null) {
            return null;
        } else {
            Log.getInstance().write(Log.LOGLEVEL_VERBOSE, "getID: Returning status " + UserRecordAPI.GetUserRecordData(record, NAME_ID));
            return UserRecordAPI.GetUserRecordData(record, NAME_ID);
        }
    }      
    
}
